$dt.ordinal = 0;
$dt.timeout = 1800;
$dt.e = {
	countdown : function() {
		if ($dt.timeout == -1) {
			$dt.e.onExamTimeout();
			return;
		}
		$('#timer-value').text($dt.timeout--);
		setTimeout($dt.e.countdown, 1000);
	},
	onExamTimeout : function() {

	},
	checkExamAvailable : function() {
		$.post('/onlineExam', {
			requestType : 'check-exam-available'
		}, function(r) {
			switch (r.startCode) {
			case 0:
			case 4:
				$dt.e.nextQuestion();
				var qacontainer = $('div.qa-panel-container');
				var timerpanel = $('#timer-panel');
				timerpanel.css('display', 'block');
				timerpanel.css('left',
						qacontainer.position().left + qacontainer.width() + 20)
						.css('top', qacontainer.position().top + 5);
				$('button.next-question').live('click', function() {
					$dt.e.nextQuestion();
					$(this).attr('disabled', 'disabled');
				});
				setTimeout($dt.e.countdown, 1000);
				break;
			case 1:
			case 2:
			case 3:
				$('#examDate').datepicker();
				break;
			default:
				break
			}
		});
	},
	nextQuestion : function() {
		if ($dt.ordinal > 5) {
			alert('exam over!');
			return;
		}
		$.post('/onlineExam', {
			requestType : 'next-question',
			ordinal : $dt.ordinal++
		}, function(r) {
			r = r[0];
			var $panel = $('div.qa-panel:first').clone();
			$panel.css('display', 'block');
			$('div.qa-panel-container').append($panel);
			$panel.find('div.q-text').text(r.question);
			$panel.data('id', r.id);
			$panel.data('examId', r.examId);
			$panel.find('div.q-logo').text('Q' + $dt.ordinal);
			for ( var i = 0; i < r.choices.length; i++) {
				var $option = $panel.find('div.a-text li:first').clone();
				$option.data('id', i);
				$option.find('label').text(r.choices[i]);
				$option.appendTo($panel.find('div.a-text ul'));
			}
			$panel.find('button:first').focus();
			$panel.find('div.a-text li:first').remove();
		});
	}
};
$(document).ready(function() {
	$dt.e.checkExamAvailable();
});